﻿using System;
using System.Collections.Generic;
using System.Text;


using Chamberlain.DAL;
using System.Data.OleDb;
using System.Data;

public class Distrito {
    public Distrito()
    {

        id = 0;
        nombre = "";
    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string nombre;

    public string Nombre
    {
        get { return nombre; }
        set { nombre = value; }
    }

    public void read()
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter();

        DataAccess da = new DataAccess();
        OleDbConnection con = da.getConnection();
        string consulta=""; 
        if (id!=0)
            consulta = "SELECT * FROM Distrito WHERE ID_Distrito=" + id.ToString();
        else if(nombre!="")
                consulta = "SELECT * FROM Distrito WHERE ID_Distrito='" + nombre + "'";
        OleDbCommand comm = new OleDbCommand(consulta, con);
        DataSet ds = null;
        adapter.SelectCommand = comm;
        OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
        ds = new DataSet("Distrito");
        adapter.Fill(ds, "Distrito");
        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            this.Id = int.Parse(dr["ID_Distrito"].ToString());

            this.nombre = dr["N_Distrito"].ToString();

        }
        da.freeResource(con);
    }

    public List<Distrito> readall()
    {
        List<Distrito> lista = new List<Distrito>();

        OleDbDataAdapter adapter = new OleDbDataAdapter();

        DataAccess da = new DataAccess();
        OleDbConnection con = da.getConnection();
        string consulta = "SELECT * FROM Distrito";
        OleDbCommand comm = new OleDbCommand(consulta, con);
        DataSet ds = null;
        adapter.SelectCommand = comm;
        OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
        ds = new DataSet("Distrito");
        adapter.Fill(ds, "Distrito");
        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            this.Id = int.Parse(dr["ID_Distrito"].ToString());

            this.nombre = dr["N_Distrito"].ToString();
            lista.Add((Distrito)this.MemberwiseClone());
        }

        da.freeResource(con);
        ds.Dispose();
        comm.Dispose();
        adapter.Dispose();
        dt.Dispose();

        return lista;
    }
}
